<template>
  <!-- 已支付详情---退费 -->
  <section>
    <div style="border-bottom:1px solid #eee;padding-bottom:10px;">
      <el-page-header @back="goBack" content="账单详情"></el-page-header>
    </div>
    <br />
    <div class="visit_detail_box">
      <div class="userinfo p-sm">
        <p>
          <img
            v-if="detail.head != ''"
            :src="detail.head | imageUrl"
            class="patient_img"
            alt
          />
          <img
            v-else
            src="/img/unknown-avatar.4f0c79ac.png"
            class="patient_img"
            alt
          />
        </p>
        <p>
          <span
            style="font-size: 20px; margin-right: 20px; vertical-align: middle;"
            >{{ detail.name }}</span
          >
          <i
            class="glyphicon glyphicon-earphone"
            style="vertical-align: middle; font-size: 18px;"
          ></i>
        </p>
        <!-- <div class="user-field">
          <span>挂账：</span>
          {{
            detail.chargeToTheRoom
              ? detail.chargeToTheRoom
              : 0 | changeMoneyToTwo
          }}
        </div> -->
        <div class="user-field">
          <span>性别：</span>
          {{ detail.sex | toSex }}
        </div>
        <div class="user-field">
          <span>年龄：</span>
          <!-- <label v-if="detail.age>7">{{detail.age}}岁</label>
          <label v-if="detail.age<7">{{detail.age}}岁{{detail.sdfMonth}}月{{detail.sdfDay}}天</label>-->
          <label v-if="detail.age > 17">{{ detail.age }}</label>
          <label v-else>{{ detail.birthday | toTimeContrast }}</label>
        </div>
        <!-- <div class="user-field">
        <span>婚姻状况：</span>
        {{detail.age}}岁
        </div>-->
        <div class="user-field">
          <span>门诊号：</span>
          <span style="color: red;">{{ detail.archivesCode }}</span>
        </div>
        <div class="user-field">
          <span>就诊日期：</span>
          <span>{{ visitTime?visitTime:detail.lastVisitTime | changeTime }}</span>
        </div>
        <div v-if="detail.chargeTime" class="user-field">
          <span>收费日期：</span>
          <span>{{ detail.chargeTime | changeTime }}</span>
        </div>
      </div>
      <div class="user-content p-sm">
        <el-row style="margin:10px 0px;display:inline-block;width:100%;">
          <el-col :span="6">
            <label style="font-size:16px;">收费项目</label>
          </el-col>
          <el-col style="height:10px;" :span="6"></el-col>
          <el-col style="height:10px;" :span="6"></el-col>
          <el-col style="height:10px;" :span="6">
            <!-- <el-button @click="opReturn()" style="float:right;">{{return_label}}</el-button> -->
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
             <table v-if="billdetail" class="table-box pay-table">
                 <tr>
                    <th>收费科室：{{billdetail.deptName==undefined?'暂无':billdetail.deptName}}</th>
                    <th>收费医生：{{billdetail.doctorName==undefined?'暂无':billdetail.doctorName}}</th>
                    <th>收费人员：{{billdetail.payMemberName==undefined?'暂无':billdetail.payMemberName}}</th>
                    <th>收费时间：{{billdetail.payTime  | changeTime }}</th>
                    <th></th>
                    <th></th>
                    <th></th>
                    <th></th>
                  </tr>
             </table>
             <!-- billdetail -->
          </el-col>
          <el-col :span="24">
            <table class="table-box">
              <thead>
                <tr>
                  <!-- <th>
                  <el-checkbox
                    :indeterminate="isIndeterminate"
                    v-model="checkAll"
                    @change="handleCheckAllChange"
                  ></el-checkbox>
                  </th>-->
                  <th>收费名称</th>
                  <th>单价</th>
                  <th>数量</th>
                  <th>单位</th>
                  <th>折扣</th>
                  <th>是否套餐抵扣</th>
                  <th>抵扣数量</th>
                  <th>金额</th>
                  <th v-if="isReturn">科室</th>
                  <th v-if="isReturn">接诊医生</th>
                </tr>
              </thead>
              <tbody>
                <tr v-for="(item, index) in itemList" :key="index">
                  <!-- <td class="return-item_td">
                  <el-checkbox-group v-model="checkedAdmin" @change="handleCheckedCitiesChange">
                    <el-checkbox :label="item.sourceId" :key="item.sourceId"></el-checkbox>
                  </el-checkbox-group>
                  </td>-->
                  <td>{{ item.sourceName }}</td>
                  <td>￥{{ item.oneMoney }}</td>
                  <td>{{ item.frequency }}</td>
                  <td>{{ item.unit | changeAssist(dwList) }}</td>
                  <td>{{ item.discount }}</td>
                  <td>{{ item.isDeduction == 0 ? "是" : "否" }}</td>
                  <td>{{ item.deductionNum || 0 }}</td>
                  <td>￥{{ item.realityMoney }}</td>
                  <td v-if="isReturn">{{ item.deptName }}</td>
                  <td v-if="isReturn">{{ item.doctorName }}</td>
                </tr>
              </tbody>
            </table>
          </el-col>
        </el-row>
        <el-row class="money_box">
          <el-col class="other" :span="18"></el-col>
          <el-col class="money" :span="6">
            <label>{{ isTf ? "实收金额" : "费用合计" }}：</label>
            <span>￥{{ totalMoney | changeMoneyToTwo }}</span>
          </el-col>
        </el-row>
        <el-row v-if="disAmount != '' && disAmount != 0" class="money_box">
          <el-col class="other" :span="18"></el-col>
          <el-col class="money" :span="6">
            <label>折扣金额：</label>
            <span>￥{{ disAmount | changeMoneyToTwo }}</span>
          </el-col>
        </el-row>
        <el-row class="money_box">
          <el-col class="other" :span="18"></el-col>
          <el-col class="money" :span="6">
            <label>{{ isTf ? "退费金额" : "应收费用" }}：</label>
            <span>￥{{ (totalMoney - disAmount) | changeMoneyToTwo }}</span>
          </el-col>
        </el-row>
        <el-row
          v-for="(item, index) in billPayways"
          :key="index"
          class="money_box"
        >
          <el-col class="other" :span="18"></el-col>
          <el-col class="money" :span="6">
            <label>
              <template v-if="item.sourceId != ''">
                <label v-for="(it, index) in item.sourceId" :key="index">
                  {{ it | changeAssist(payTypeList) }}
                  <label v-if="index < item.sourceId.length - 1">、</label>
                </label>
              </template>
              {{ item.source | changeSourceName }}：
            </label>
            <span>￥{{ item.payMoney | changeMoneyToTwo }}</span>
          </el-col>
        </el-row>
        <el-row v-if="showBill">
          <el-button style="width:100%;margin-top:20px" @click="doPrint"
            >打印票据</el-button
          >
        </el-row>
        <el-row v-if="isReturn" style="margin-top:20px;">
          <el-button style="float:right;" @click="openDetail()">退费</el-button>
        </el-row>
      </div>
      <!-- <printview v-if="showprint" :item="itemList" :orderId="orderId" :detail="detail" :total="totalMoney" :name="name"></printview> -->
      <iframe
        v-if="showprint"
        frameborder="0"
        height="0px"
        width="0px"
        ref="iframe"
        src="/print/chargerecipe.html"
      ></iframe>
      <iframe
        v-if="showreturn"
        frameborder="0"
        height="0px"
        width="0px"
        ref="iframe"
        src="/print/returnrecipe.html"
      ></iframe>
    </div>
  </section>
</template>
<script>
export default {
  name: "chargeDetail",
  data() {
    return {
      dialogRecord: false,
      detail:{
        chargeTime:''
      },
      checkedAdmin: [],
      checkAll: [],
      isIndeterminate: true,
      itemList: [],
      reductionMoney: 0,
      return_label: "退费",
      returnMoney: 0,
      iframeContent: {},
      total: 0,
      totalMoney: 0, //总金额
      billPayways: [],
      dwList: [],
      disAmount: 0,
      integralNum: 0,
      doctorId: 0,
      deptId: 0,
      doctor: "",
      jfUse: "",
      name: "",
      orderId: "",
      isReturn: true,
      showBill: false,
      showprint: false,
      showreturn: false,
      isTf: false,
      payTypeList: [],
      hpMember: "",
      billdetail:"",
      visitTime:''
    };
  },
  created() {
    this.getIntegralRuleById();
  },
  mounted() {
    let th = this;
    let type = th.$route.query.type;
    th.orderId = th.$route.query.orderId;
    if (th.$route.query.type != undefined) {
      th.isReturn = false;
      if (type == 1) {
        th.showBill = true;
      }
    }
    if (th.$route.query.isReturn != undefined) {
      th.isTf = true;
    }
    th.getBasic();
    th.findUsUserById();
    th.getItemDetail();
  },
  methods: {
    findUsUserById() {
      let th = this;
      th.showLoading();
      let uid = th.jieMi(th.$route.query.patientId);
      th.$api
        .getThePatientLibraryById({ patientId: uid, hpId: th.adminUser.hpId })
        .then(res => {
          if (res.executed) {
            th.showLoading(false);
            th.detail = res.thePatientLibraryDetail;
            th.active = true;
            th.ispersonal = false;
          }
        });
    },
    getItemDetail() {
      let th = this;
      let id = th.jieMi(th.$route.query.id);
      if (th.isTf == true) {
        //退费
        th.$api.getRefimdRecordDetail({ refundId: id }).then(res => {
          if (res.executed) {
            let _check = [];
            let list = res.dataBody.getOrderItemInfos;
            //收费类别
            let bill = res.dataBody.billPayways;
            if (bill != undefined) {
              bill.map(it => {
                if (it.sourceId) {
                  it.sourceId = it.sourceId.split(",");
                }
              });
              th.detail.chargeTime = bill[0].gmtCreate;
              th.billPayways = bill;
            }
            let doctor = res.dataBody.doctor;
            th.doctor = doctor;
            th.hpMember = res.dataBody.hpMember;
            let _item = [];
            let total = 0;
            for (let i = 0; i < list.length; i++) {
              let arr = {};
              arr = Object.assign(arr, list[i], list[i].cureDetailDTO);
              _check.push(list[i].sourceId);
              total += Number(arr.totalMoney);
              _item.push(arr);
            }
            th.totalMoney = total.toFixed(2);
            th.total = total.toFixed(2);
            if (th.total > th.jfUse.useNum) {
              let _total = parseInt(th.total);
              th.integralNum =
                Math.ceil(_total / Number(th.jfUse.useNum)) * th.jfUse.getNum;
            } else {
              th.integralNum = 0;
            }
            th.checkedAdmin = _check;
            th.itemList = _item;
          }
        });
      } else {
        if (th.isReturn == true) {
          //订单详情待收费
          th.$api.findNoPayDetailListByNew({ cureId: id }).then(res => {
            if (res.executed) {
              let item = res.data;
              let list = [];
              let _check = [];
              let total = 0;
              th.deptId = item[0].deptId;
              th.doctorId = item[0].doctorId;
              if (res.data.professionalDeptName) {
                let doctor = {
                  professionalDeptName: res.data.professionalDeptName,
                  trueName: res.data.doctorName
                };
                th.doctor = doctor;
              }
              for (let i = 0; i < item.length; i++) {
                let arr = {
                  discount: 100,
                  realityMoney: "",
                  discountMoney: ""
                };
                arr = Object.assign(arr, item[i]);
                arr.realityMoney = Number(item[i].totalMoney).toFixed(2);
                arr.discountMoney = 0;
                _check.push(item[i].sourceId);
                total += Number(item[i].totalMoney);
                list.push(arr);
              }
              th.totalMoney = total.toFixed(2);
              th.total = total.toFixed(2);
              if (th.total > th.jfUse.useNum) {
                let _total = parseInt(th.total);
                th.integralNum =
                  Math.ceil(_total / Number(th.jfUse.useNum)) * th.jfUse.getNum;
              } else {
                th.integralNum = 0;
              }
              th.checkedAdmin = _check;
              th.itemList = list;
            }
          });
        } else {
          //收费详情---已收费
          th.$api
            .getOrderItem({ orderId: th.$route.query.orderId })
            .then(res => {
              if (res.executed) {
                let _check = [];
                let list = res.dataBody.pageInfo.list;
                let bill = res.dataBody.billPayways;
                let doctor = res.dataBody.doctor;
                bill.map(it => {
                  if (it.sourceId) {
                    it.sourceId = it.sourceId.split(",");
                  }
                });
                 
                th.billPayways = bill;
                if (bill.length > 0) {
                  th.detail.chargeTime = res.dataBody.collectFeeTime;
                  let billdetail={
                    deptName:res.dataBody.deptName,
                    doctorName:res.dataBody.doctorName,
                    payMemberName:res.dataBody.payMemberName,
                    payTime:bill[0].gmtCreate
                  }
                  this.billdetail=billdetail;
                }
                th.doctor = doctor;
                let _item = [];
                let total = 0;
                let _discount=0;
               _item=list.map(it=>{
                  let arr={...it,...it.cureDetailDTO};
                  // arr = Object.assign(arr, it, it.cureDetailDTO);
                  _check.push(it.sourceId);
                  arr.isDeduction=it.isWriteOff==0?1:0;
                  total += Number(arr.totalMoney);
                  _discount+=Number(arr.discountMoney);
                  return arr;
                })
                th.totalMoney = total.toFixed(2);
                th.total = total.toFixed(2);
                th.disAmount=_discount.toFixed(2);
                if (th.total > th.jfUse.useNum) {
                  let _total = parseInt(th.total);
                  th.integralNum =
                    Math.ceil(_total / Number(th.jfUse.useNum)) *
                    th.jfUse.getNum;
                } else {
                  th.integralNum = 0;
                }
                th.visitTime= res.dataBody.visitTime
                th.checkedAdmin = _check;
                th.itemList = _item;
              }
            });
        }
      }
    },
    getBasic() {
      let th = this;
      th.getCommonConfigByKey("药品剂量单位").then(res => {
        th.dwList = res;
      });
    },
    //修改折扣重新计算金额
    changeDis(item) {
      let th = this;
      let dis = item.discount;
      let oldMoney = Number(item.realityMoney);
      item.realityMoney = Number(
        item.totalMoney * (item.discount / 100)
      ).toFixed(2);
      item.discountMoney = Number(item.totalMoney - item.realityMoney).toFixed(
        2
      );
      th.totalMoney =
        Number(th.totalMoney) - Number(oldMoney) + Number(item.realityMoney);
      if (th.totalMoney > th.jfUse.useNum) {
        let _total = parseInt(th.totalMoney);
        th.integralNum =
          Math.ceil(_total / Number(th.jfUse.useNum)) * th.jfUse.getNum;
      } else {
        th.integralNum = 0;
      }
    },
    openDetail() {
      let th = this;
      th.$router.push({
        path: "/page/chargeReturn",
        query: th.getUrlToken({
          id: th.$route.query.id,
          orderId: th.$route.query.orderId,
          patientId: th.$route.query.patientId
        })
      });
    },
    //获取积分规则
    getIntegralRuleById() {
      let th = this;
      th.$api
        .getIntegralRuleById({ hpId: th.adminUser.hpId, type: 1 })
        .then(res => {
          if (res.executed) {
            th.jfUse = res.integralRuleList[0];
          }
        });
      th.getCommonConfigByKey("收费减免类型").then(res => {
        th.payTypeList = res;
      });
    },
    goBack() {
      this.$router.go(-1);
    },
    doPrint() {
      let th = this;
      if (th.isTf) {
        th.showreturn = true;
      } else {
        th.showprint = true;
      }
      let specialList = [];
      let item = [];
      specialList = th.itemList.filter(it => {
        if (it.source == 13) {
          return it;
        }
      });
      item = th.itemList.filter(it => {
        if (it.source != 13) {
          return it;
        }
      });
      let deptName = "";
      let doctorName = "";
      let memberName = "";
      if (th.doctor) {
        deptName = th.doctor.professionalDeptName;
        doctorName = th.doctor.trueName;
      }
      if (th.hpMember) {
        memberName = th.hpMember.memberName;
      }
      setTimeout(function() {
        th.iframeContent = th.$refs.iframe.contentWindow;
        th.iframeContent.postMessage(
          {
            cmd: "postJson",
            params: JSON.stringify({
              detail: th.detail,
              disAmount: th.disAmount,
              list: item,
              doctor: doctorName || "",
              dept: deptName || "",
              memberName: memberName || "",
              special: specialList, //特殊收费
              billPayways: th.billPayways,
              logo: th.adminUser.printLogo,
              orderCode: th.$route.query.orderCode,
              total: th.totalMoney,
              hospitalName: th.adminUser.hospitalName
            })
          },
          "*"
        );
      }, 500);
    }
  },
  components: {
    // printview: () => import("../_com/printFp")
  },
  filters: {}
};
</script>
<style lang="scss" scoped>
@import "../_css/detail.scss";
.patient_img {
  border: 1px solid #ddd;
  border-radius: 5px;
  display: inline-block;
  width: 100px;
  height: 100px;
}
.durg_title {
  display: inline-block;
  width: 100%;
  font-size: 16px;
  border-bottom: 1px solid #ddd;
  padding-bottom: 10px;
  margin-bottom: 10px;
}

.money_box {
  margin-top: 10px;
  display: inline-block;
  width: 100%;
  .other {
    height: 10px;
    display: inline-block;
  }
  .money {
    text-align: right;
    padding-right: 10px;
    display: inline-block;
  }
}
.printOrder {
  display: inline-block;
  width: 100%;
}
.pay-table{
  text-align: left;
  margin-bottom: 10px;
  font-size: 15px;
}
</style>
